package com.itqf.mybatis;

import com.itqf.bean.Student;
import com.itqf.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * projectName: mybatis2103
 *
 * @author: 赵伟风
 * time: 2021/6/24 10:14
 * description:
 */
public class UseMybatisInsert {

    public static void main(String[] args) throws Exception{


        //1.读取配置
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2.创建sqlsesssionfactorybuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3.创建sqlsessionfactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4.创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student student = new Student();
//        String id = UUID.randomUUID().toString().replaceAll("-", "");
//        student.setsId(id);
        student.setsName("张三子");

        System.out.println("student 前= " + student);

        mapper.insertStudent(student);

        System.out.println("student 后= " + student);
        //7.关闭资源
        sqlSession.commit();
        sqlSession.close();

    }
}
